import React, { useState } from 'react'
import { PaginationComponetProps } from '@/types'

export interface TableState {
  dataSource: any[]
  setDataSource: (dataSource: any[]) => void
  selectedRowKeys: React.Key[]
  selectedRows: any[]
  setSelection: (selectedRowKeys: React.Key[], selectedRows: any[]) => void
  pagination: PaginationComponetProps
  setPagination: (pagination: Partial<PaginationComponetProps>) => void
}

/**
 * 表格状态
 */
export const useTableState = (): TableState => {
  const [dataSource, setDataSource] = useState<any[]>([])
  const [selectedRowKeys, setSelectedRowKeys] = useState<React.Key[]>([])
  const [selectedRows, setSelectedRows] = useState<any[]>([])
  const [pagination, setPaginationActual] = useState<PaginationComponetProps>({
    pageNumber: 1,
    pageSize: 10,
    total: 0,
  })

  const setSelection = (selectedRowKeys: React.Key[], selectedRows: any[]) => {
    setSelectedRowKeys(selectedRowKeys)
    setSelectedRows(selectedRows)
  }

  const setPagination = (pagination: Partial<PaginationComponetProps>) => {
    setPaginationActual((prev) => ({ ...prev, ...pagination }))
  }

  return {
    dataSource,
    setDataSource,
    selectedRowKeys,
    selectedRows,
    setSelection,
    pagination,
    setPagination,
  }
}
